package com.zy.config;

import com.zy.security.UserDetailsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    UserDetailsServiceImpl userDetailsService;

    /**
     * 权限管理
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 访问权限配置
        http.authorizeRequests()
                .antMatchers("/", "/app/**").permitAll()
                .antMatchers("/register", "/register*").permitAll()
                .anyRequest().authenticated();


        // 自定义登录页
        http.formLogin()
                .usernameParameter("username") //表单username
                .passwordParameter("password") //表单password
                .loginPage("/") //自定义登录页面
                .loginProcessingUrl("/toLogin") //登陆表单提交请求的地址
                .defaultSuccessUrl("/menu") //登录成功后跳转的页面
                .failureUrl("/loginError") //登录失败后跳转的页面
                .permitAll()
                .and()
                .exceptionHandling().accessDeniedPage("/error403.html"); //自定义403权限不足的页面

        //自定义注销
        http.logout()
                .logoutUrl("/logout") //访问该地址注销
                .logoutSuccessUrl("/") //注销后重定向到该地址
                .deleteCookies("JESSIONID") //清除cookie
                .invalidateHttpSession(true); //使HttpSession失效

        //禁用CSRF保护
        http.csrf().disable();

        //允许同源嵌套
        http.headers().frameOptions().sameOrigin();
    }

    /**
     * 认证
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    /**
     * 加密编码器
     */
    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
     * 解决静态资源被拦截的问题
     *
     * @param web
     * @throws Exception
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
                .antMatchers("/druid/**", "/css/**", "/js/**", "/image/**", "/info/**");
    }

    /**
     * security + freemarker
     *
     * @return
     */
    @Bean
    public ClassPathTldsLoader classPathTldsLoader() {
        return new ClassPathTldsLoader();
    }
}

